package io.xuan.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xuan.common.exception.LinfengException;
import io.xuan.common.utils.PageUtils;
import io.xuan.common.utils.Query;
import io.xuan.modules.app.dao.CarInfoDao;
import io.xuan.modules.app.entity.CarInfoEntity;
import io.xuan.modules.app.service.CarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("carInfoService")
public class CarInfoServiceImpl extends ServiceImpl<CarInfoDao, CarInfoEntity> implements CarInfoService {
    public List<CarInfoEntity> getList() {
        return list();
    }


    @Autowired
    private CarInfoDao carInfoDao;

    public void updateCarLocation(Map<String, String> map) {
//        for (String test : map.keySet()) {
//            System.out.println(test);
//            System.out.println(map.get(test));
//        }
        carInfoDao.updateCarLocation(map);
    }

    public List<CarInfoEntity> AllCarInfo(Integer lineId) {
        return carInfoDao.search(lineId);
    }

    public List<Map<String, Object>> carLocation(Integer lineId) {

        return carInfoDao.getCarLocationsByLineId(lineId);
    }

    @Override
    public CarInfoEntity getCarInfo(int carId) {
        return carInfoDao.getCarInfo(carId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarInfoEntity> page = this.page(
                new Query<CarInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCarInfo(CarInfoEntity carInfo) {
        Integer count = this.lambdaQuery()
                .eq(CarInfoEntity::getCarId, carInfo.getCarId())
                .count();
        if (count > 0) {
            throw new LinfengException("名不能重复");
        }
        boolean save = this.save(carInfo);
        if (!save) {
            throw new LinfengException("保存失败");
        }
    }

    @Override
    public void deleteByIdList(List<Integer> list) {
        this.removeByIds(list);
    }

    @Override
    public void updateCarInfo(CarInfoEntity carInfo) {
        Integer count = this.lambdaQuery()
                .eq(CarInfoEntity::getCarId, carInfo.getCarId())
                .count();
        if (count > 1) {
            throw new LinfengException("名不能重复");
        }
        boolean update = this.updateById(carInfo);
        if (!update) {
            throw new LinfengException("更新失败");
        }
    }

}
